﻿package mlc.avm2.visualizing;
import flash.display.Stage;
import flash.display.StageAlign;
import hsl.haxe.data.mathematics.Point;

/**
 * This repositioner repositions the view of the console visualizer, based on the stage align setting, so it always fully
 * covers the screen.
 */
class AVM2ConsoleVisualizerViewRepositioner {
	private var originalStageDimensions:Point;
	private var stage:Stage;
	/**
	 * Creates a new AVM2 console visualizer view repositioner.
	 */
	public function new(stage:Stage, originalStageDimensions:Point):Void {
		this.stage = stage;
		// If no original stage dimensions are passed, use the current stage dimensions. This will not work properly in some
		// cases.
		this.originalStageDimensions =
			if (null == originalStageDimensions) {
				new Point(stage.stageWidth, stage.stageHeight);
			} else {
				originalStageDimensions;
			}
	}
	public function reposition(target:AVM2ConsoleVisualizerView):Void {
		// If the stage aligns top-right, no repositioning is needed.
		if (StageAlign.TOP_RIGHT == stage.align) {
			return;
		}
		var deltaX:Float = originalStageDimensions.x - stage.stageWidth;
		var deltaY:Float = originalStageDimensions.y - stage.stageHeight;
		// Replace the view based on the way the stage aligns.
		switch (stage.align) {
			case StageAlign.BOTTOM:
			target.x = deltaX * .5;
			target.y = deltaY;
			case StageAlign.BOTTOM_LEFT:
			target.y = deltaY;
			case StageAlign.BOTTOM_RIGHT:
			target.x = deltaX;
			target.y = deltaY;
			case StageAlign.LEFT:
			target.y = deltaY * .5;
			case StageAlign.RIGHT:
			target.x = deltaX;
			target.y = deltaY * .5;
			case StageAlign.TOP:
			target.x = deltaX * .5;
			case StageAlign.TOP_RIGHT:
			target.x = deltaX;
			case StageAlign.TOP_LEFT:
		}
	}
}